package splug.cjk;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import splug.cjk.packet.FriendPacket;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class FriendTab2 extends ListActivity implements OnItemClickListener{

	private String id;
	private ArrayList<String> content = new ArrayList<String>();
	private ArrayAdapter<String> m_adapter;
	private boolean[] pos = new boolean[100];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend_tab2);

		final ListView lv = getListView();

		for(int i = 0; i < pos.length; i++){
			pos[i] = false;
		}

		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");

		Button btn_accept = (Button) findViewById(R.id.btn_friend_accept);

		btn_accept.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				for(int i = 0; i < pos.length; i++){
					if(pos[i]){
						try {
							Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

							InputStream is = socket.getInputStream();
							OutputStream os = socket.getOutputStream();

							OutputStreamWriter osw = new OutputStreamWriter(os);
							InputStreamReader isr = new InputStreamReader(is);

							osw.write(15);
							osw.flush();

							int ack = isr.read();

							if(ack == '1'){
								FriendPacket packet = new FriendPacket(null, id, content.get(i));
								ObjectOutputStream output = new ObjectOutputStream(os);
								output.writeObject(packet);

								ack = isr.read();
								
								if(ack == '1'){
									Toast.makeText(FriendTab2.this, "수락완료!", Toast.LENGTH_SHORT).show();
									
								}
							}
							else{
								Toast.makeText(FriendTab2.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
							}
							
							
						} catch (UnknownHostException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				
				try {
					Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

					InputStream is = socket.getInputStream();
					OutputStream os = socket.getOutputStream();

					OutputStreamWriter osw = new OutputStreamWriter(os);
					InputStreamReader isr = new InputStreamReader(is);

					osw.write(14);
					osw.flush();

					int ack = isr.read();

					if(ack == '1'){
						FriendPacket packet = new FriendPacket(null, id, "");
						ObjectOutputStream output = new ObjectOutputStream(os);
						output.writeObject(packet);

						ObjectInputStream input = new ObjectInputStream(is);
						packet = (FriendPacket) input.readObject();

						content = packet.getFriend_list();
					}
					else{
						Toast.makeText(FriendTab2.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
					}


				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}

				m_adapter = new ArrayAdapter<String>(FriendTab2.this, android.R.layout.simple_list_item_multiple_choice, content);
				lv.setAdapter(m_adapter);
				lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
			}
		});

		try {
			Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();

			OutputStreamWriter osw = new OutputStreamWriter(os);
			InputStreamReader isr = new InputStreamReader(is);

			osw.write(14);
			osw.flush();

			int ack = isr.read();

			if(ack == '1'){
				FriendPacket packet = new FriendPacket(null, id, "");
				ObjectOutputStream output = new ObjectOutputStream(os);
				output.writeObject(packet);

				ObjectInputStream input = new ObjectInputStream(is);
				packet = (FriendPacket) input.readObject();

				content = packet.getFriend_list();
			}
			else{
				Toast.makeText(FriendTab2.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
			}


		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		m_adapter = new ArrayAdapter<String>(FriendTab2.this, android.R.layout.simple_list_item_multiple_choice, content);
		lv.setAdapter(m_adapter);
		lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}

	@Override
	public void onItemClick(AdapterView parent, View view, int position, long id) {
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		if(pos[position])
			pos[position] = false;
		else
			pos[position] = true;
	}



}
